/*
* Filename:recdialog.c
* Summary:	
*
* Author:
* Version:
* Finished time:
*/
#include "head.h"
#include "config.h"
#include "ipmsg.h"
#include "recdialog.h"
#include "mainwindow.h"
#include "fileselection.h"
#include "utility.h"
/*
*global
*/
struct RecvFileInfo recv_file;
GtkWidget *recdialog;
GtkWidget *recvfile_button;
/*
*get file info
*/
void GetRecvFileInfo(char *fileinfo, char *filename, char *hex_mtime, char *file_sz)
{
	char *temp = strchr(fileinfo, ':');
	char *temp1 =strnchr(fileinfo, ':', 2);
	int name_sz = temp1 - temp - 1;
#ifdef _DEBUG
	printf("The sz of filesize is:%d\n", name_sz);
#endif
	name_sz > MAX_NAME_SZ ? MAX_NAME_SZ : name_sz;
	strncpy(filename, temp + 1, name_sz);

	temp = strnchr(fileinfo, ':', 2);
	temp1 = strnchr(fileinfo, ':', 3);
	int filesz_sz = temp1 - temp - 1;
	filesz_sz > MAX_FILELEN ? MAX_FILELEN : filesz_sz;
	strncpy(file_sz, temp + 1, filesz_sz);

	temp = strnchr(fileinfo, ':', 3);
	temp1 = strnchr(fileinfo, ':', 4);
	int mtime_sz = temp1 - temp - 1;
	mtime_sz > MAX_HEX_MTIME ? MAX_HEX_MTIME : mtime_sz;
	strncpy(hex_mtime, temp + 1, mtime_sz);
}
/*
*check and select the message-sender
*/
gboolean CheckSender(GtkTreeModel *model, GtkTreePath *path,
					 GtkTreeIter *iter, gpointer data)
{
#ifdef _DEBUG
	printf("Enter CheckSender\n");
#endif
	gchar *ipaddress;
	gchar *temp = (gchar*)data;
	gtk_tree_model_get(model, iter, IPADDRESS, &ipaddress, -1);

#ifdef _DEBUG
	printf("Get from model:%s\n, From outside:%s\n", ipaddress, temp);
#endif

	int i = strcmp(ipaddress, temp);
	if ( i == 0 )//find the sender
	{
#ifdef _DEBUG
		printf("Find the message-sender\n");
#endif
		gtk_tree_selection_select_path(selection, path);
	} else
	{
		gtk_tree_selection_unselect_path(selection, path);
	}
	free(ipaddress);
	return FALSE;
}

/*
funciton for reply and close button
*/
void FuncForResponse(GtkDialog *dialog, gint response_id, gpointer user_data)
{

	struct sockaddr_in *from = user_data; 
	if ( response_id == GTK_RESPONSE_OK )//if user click reply button
	{
		gtk_widget_destroy(GTK_WIDGET(dialog));
		//find and select the message sender
		gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), CheckSender, user_data);
		//show the main_window to reply
		gtk_widget_show_all(GTK_WIDGET(main_window));
		gtk_widget_hide(GTK_WIDGET(file_button));
	} else if ( response_id == GTK_RESPONSE_CLOSE )//if usr click close button
	{
		gtk_widget_destroy(GTK_WIDGET(dialog));

	}
}
/*
*When a message or a message with file come, show the recvdialog
*/
void MakeRecDialog(struct MessageFormat *mess, struct sockaddr_in *from)
{


	GtkWidget *up_hbox;
	GtkWidget *mid_hbox;
	GtkWidget *down_hbox;

	GtkWidget *frame;
	GtkWidget *scrolledwindow;


	GtkWidget *vbox1;
	GtkWidget *textview;
	GtkTextBuffer *textbuffer;//invisable widget

	GtkWidget *up_hbox1;
	GtkWidget *down_hbox1;

	GtkWidget *from_label;
	GtkWidget *loginname_label;
	GtkWidget *at_label;
	GtkWidget *hostname_label;
	GtkWidget *time_label;

	gchar *login_name;
	gchar *host_name;
	gchar *message;
	//charset convertion
	login_name = ConvertFrom(mess->login_name);
	host_name = ConvertFrom(mess->host_name);
	message = ConvertFrom(mess->append_message);
	char *ipaddress = inet_ntoa(from->sin_addr);
#ifdef _DEBUG
	g_print("receive sended message:%s\n", message);
#endif



	//create objects
	/*first create the dialog with buttons*/
	recdialog = gtk_dialog_new_with_buttons(_("Message Received"), main_window,
											GTK_DIALOG_DESTROY_WITH_PARENT,
											_("reply"), GTK_RESPONSE_OK,
											_("close"), GTK_RESPONSE_CLOSE,
											NULL);

	from_label = gtk_label_new(_("from:"));
	loginname_label = gtk_label_new(login_name);
	at_label = gtk_label_new("@");
	hostname_label = gtk_label_new(host_name);
	time_t now= time(NULL);
	struct tm *tm_now = localtime(&now);
	char *ascnow = asctime(tm_now);
	time_label = gtk_label_new(ascnow);

	up_hbox1 = gtk_hbox_new(FALSE, 0);
	down_hbox1 = gtk_hbox_new(FALSE, 0);

	vbox1 = gtk_vbox_new(FALSE, 0);
	textview = gtk_text_view_new();
	textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
	GtkTextIter start;
	gtk_text_buffer_get_start_iter (textbuffer, &start);
	gtk_text_buffer_insert(textbuffer, &start, 
						   message, -1);


	frame = gtk_frame_new(_("From"));
	scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
	recvfile_button = gtk_button_new();

	up_hbox = gtk_hbox_new(FALSE, 0);
	mid_hbox = gtk_hbox_new(FALSE, 0);
	down_hbox = gtk_hbox_new(FALSE, 0);

	//pack objects
	gtk_box_pack_start(GTK_BOX(up_hbox1), from_label, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(up_hbox1), loginname_label, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(up_hbox1), at_label, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(up_hbox1), hostname_label, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(down_hbox1), time_label, FALSE, FALSE, 10);

	gtk_box_pack_start(GTK_BOX(vbox1), up_hbox1, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(vbox1), down_hbox1, FALSE, FALSE, 0);

	gtk_container_add(GTK_CONTAINER(frame), vbox1);
	gtk_container_add(GTK_CONTAINER(scrolledwindow), textview);

	gtk_container_add(GTK_CONTAINER(up_hbox), frame);
	gtk_container_add(GTK_CONTAINER(mid_hbox), recvfile_button);
	gtk_container_add(GTK_CONTAINER(down_hbox), scrolledwindow);

	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(recdialog)->vbox), up_hbox, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(recdialog)->vbox), mid_hbox,  FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(recdialog)->vbox), down_hbox, TRUE, TRUE, 5);

	//adjust objects
	//dialog,button
	gtk_widget_set_usize(GTK_WIDGET(recdialog), 300, 300);
	gtk_window_set_resizable(GTK_WINDOW(recdialog), FALSE);
	gtk_button_set_relief(recvfile_button, GTK_RELIEF_NONE);
	//frame
	//scrolledwindow
	gtk_widget_set_usize(GTK_WIDGET(scrolledwindow), 300, 225);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), 
								   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	//text_label
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_CHAR);
	gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
	//action area
	gtk_button_box_set_layout(GTK_DIALOG(recdialog)->action_area, GTK_BUTTONBOX_SPREAD);
	//signal connect
	gtk_signal_connect(GTK_OBJECT(recdialog), "response", 
					   G_CALLBACK(FuncForResponse), (gpointer)ipaddress);
	//show objects
	gtk_widget_show_all(recdialog);
	gtk_widget_hide(recvfile_button);

	/*Add file receiving ability*/

	char filename[MAX_NAME_SZ];
	char hex_mtime[MAX_HEX_MTIME];
	char hex_seq_num[MAX_HEX_SEQ];
	char file_sz[MAX_FILELEN];
	memset(filename, '\0', MAX_NAME_SZ);
	memset(hex_mtime, '\0', MAX_HEX_MTIME);
	memset(hex_seq_num, '\0', MAX_HEX_SEQ);
	memset(file_sz, '\0', MAX_FILELEN);
	if ( mess->cmd&IPMSG_FILEATTACHOPT )
	{
		GetRecvFileInfo(mess->file_info, filename, hex_mtime, file_sz);
		memset(&recv_file, '\0', sizeof(struct RecvFileInfo));
		//set recvfile's seqnum
		sprintf(hex_seq_num, "%x", mess->seqnum);
		strncpy(recv_file.hex_seq_num, hex_seq_num, strlen(hex_seq_num));
		//set 32 bit address
		recv_file.sender_ip = from->sin_addr;
		//set recvfile's size
		recv_file.file_sz = strtol(file_sz, NULL, 16);
		//set recvfile's name
		recv_file.file_name = ConvertFrom(filename);
		gtk_signal_connect(GTK_OBJECT(recvfile_button), "clicked", MakeRecvSelection, from);
		gtk_button_set_label(recvfile_button, recv_file.file_name);
#ifdef _DEBUG
		printf("The received filename:%s,filenamesize:%d\n", recv_file.file_name, strlen(filename));
#endif
		gtk_widget_show(recvfile_button);
	}
	//free
	free(login_name);
	free(host_name);
	free(message);

}

